package com.ww.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("Role");
        if (!role.equals("admin")){
            //管理员后台拦截器
            try {
                response.sendRedirect("/pages/signin.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            //身份为管理员，放行
            return true;
        }
        return false;
    }
}
